package com.hetao.xx.orm;

import android.util.Log;

import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.List;

public abstract class AbstractDao<T> {

    protected final String Tag= getClass().getSimpleName();

    protected abstract Dao<T, Integer> getDao() throws SQLException;

    public T queryById(int id) {
        try {
            return getDao().queryForId(id);
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return null;
    }

    public int add(T bean) {
        try {
            return getDao().create(bean);
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }

    public int update(T bean) {
        try {
            return getDao().update(bean);
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }

    public List<T> queryAll() {
        try {
            return getDao().queryForAll();
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return null;
    }

    public long countAll() {
        try {
            return getDao().countOf();
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }

    public int delById(int id) {
        try {
            return getDao().deleteById(id);
        }catch (Exception e) {
            Log.e(Tag, e.getMessage());
        }
        return 0;
    }
}
